package com.teapos.web;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.teapos.enumtype.TradeTypeEnum;
import com.teapos.service.AdvancePayService;
import com.teapos.service.BuyBackService;
import com.teapos.service.TradeService;
import com.teapos.util.Maps;
import com.teapos.util.Tool;

@SuppressWarnings({"rawtypes", "unchecked"})
@RequestMapping("/trade")
@Controller
public class TradeController extends BaseController {
	
	@Autowired
	private TradeService tradeService;
	@Autowired
	private BuyBackService buyBackService;
	@Autowired
	private AdvancePayService advancePayService;

	@RequestMapping("/index")
	public String index(Map map) {
		//Maps.put(map, "tradeTypes", TradeTypeEnum.values());
		return "trade/index";
	}
	
	@RequestMapping("/scat")
	public String scat(HttpServletRequest req, Map map) {
		List stores = super.getUserStores(req);
		Map param = super.getParaMap(req);
		if(stores.size()==1) param.put("storeId", ((Map)stores.get(0)).get("store_id"));
		//System.err.println(param);
		param.put("type", TradeTypeEnum.SCAT.getCode());
		map.put("orders", tradeService.getOrders(param));
		map.put("page", Maps.newHashMap("pageNo", Tool.initPageNo(req.getParameter("pageNo")), "total",
				tradeService.getOrderCount(param), "js", "turnpage"));
		map.put("query", Tool.dealParam(param));
		//System.err.println(Tool.dealParam(param));
		return "trade/scat";
	}
	
	@RequestMapping("/scat_detail")
	public String scatDetail(String print,String orderId, String customerId, Map map) {
		Map detail = tradeService.getOrderDetails(orderId, customerId);
		map.put("detail", detail);
		if( "1".equals(print) ) {
			return "print/print_scat_detail";
		} else {
			return "trade/scat_detail";
		}
	}
	
	@RequestMapping("/cancel_list")
	public String cancel_list(HttpServletRequest req, Map map) {
		map.put("tradeTypes", TradeTypeEnum.values());
		List stores = super.getUserStores(req);
		Map param = super.getParaMap(req);
		if(stores.size()==1) param.put("storeId", ((Map)stores.get(0)).get("store_id"));
		map.put("orders", tradeService.getOrders(Maps.put(param, "query_type", "cancel")));
		map.put("page", Maps.newHashMap("pageNo", Tool.initPageNo(req.getParameter("pageNo")), "total",
				tradeService.getOrderCount(param), "js", "turnpage"));
		map.put("query", Tool.dealParam(param));
		return "trade/cancel";
	}
	
	@RequestMapping("/cancel_detail")
	public String cancel_detail(String orderId, String customerId, Map map) {
		Map detail = tradeService.getOrderAndDetail(orderId, customerId);
		map.put("detail", detail);
		return "trade/cancel_detail";
	}
	
	@RequestMapping("/buyback")
	public String buyback(HttpServletRequest req, Map map) {
		map.put("tradeTypes", TradeTypeEnum.values());
		List stores = super.getUserStores(req);
		Map param = super.getParaMap(req);
		if(stores.size()==1) param.put("storeId", ((Map)stores.get(0)).get("store_id"));
		map.put("orders", tradeService.getOrders(Maps.put(param, "type", TradeTypeEnum.BUY.getCode())));
		map.put("page", Maps.newHashMap("pageNo", Tool.initPageNo(req.getParameter("pageNo")), "total",
				tradeService.getOrderCount(param), "js", "turnpage"));
		return "trade/buyback";
	}
	
	@RequestMapping("/buyback_list")
	public String buyback_list(HttpServletRequest req, Map map) {
		List stores = super.getUserStores(req);
		Map param = super.getParaMap(req);
		if(stores.size()==1) param.put("storeId", ((Map)stores.get(0)).get("store_id"));
		map.put("orders", tradeService.getOrders(Maps.put(param, "type", TradeTypeEnum.BUY.getCode())));
		map.put("page", Maps.newHashMap("pageNo", Tool.initPageNo(req.getParameter("pageNo")), "total",
				tradeService.getOrderCount(param), "js", "turnpage"));
		return "trade/buyback_list";
	}
	
	@RequestMapping("/buyback_detail")
	public String buyback_detail(String orderId, String customerId, Map map) {
		Map detail = buyBackService.getOrderDetail(orderId, customerId);
		map.put("detail", detail);
		return "trade/buyback_detail";
	}
	
	@RequestMapping("/advance")
	public String advance(HttpServletRequest req, Map map) {
		map.put("tradeTypes", TradeTypeEnum.values());
		List stores = super.getUserStores(req);
		Map param = super.getParaMap(req);
		if(stores.size()==1) param.put("storeId", ((Map)stores.get(0)).get("store_id"));
		map.put("orders", tradeService.getOrders(Maps.put(param, "type", TradeTypeEnum.BEFORE.getCode())));
		map.put("page", Maps.newHashMap("pageNo", Tool.initPageNo(req.getParameter("pageNo")), "total",
				tradeService.getOrderCount(param), "js", "turnpage"));
		return "trade/advance";
	}
	
	@RequestMapping("/advance_list")
	public String advance_list(HttpServletRequest req, Map map) {
		List stores = super.getUserStores(req);
		Map param = super.getParaMap(req);
		if(stores.size()==1) param.put("storeId", ((Map)stores.get(0)).get("store_id"));
		map.put("orders", tradeService.getOrders(Maps.put(param, "type", TradeTypeEnum.BEFORE.getCode())));
		map.put("page", Maps.newHashMap("pageNo", Tool.initPageNo(req.getParameter("pageNo")), "total",
				tradeService.getOrderCount(param), "js", "turnpage"));
		return "trade/advance_list";
	}
	
	@RequestMapping("/advance_detail")
	public String advance_detail(String print,String orderId, String customerId, Map map) {
		Map detail = advancePayService.getOrderDetail(orderId, customerId);
		//System.err.println(detail);
		map.put("detail", detail);
		if( "1".equals(print) ) {
			return "print/print_advance_detail";
		} else {
			return "trade/advance_detail";
		}
	}
	
	@RequestMapping("/advance_pay")
	public String advance_pay(String id, Map map) {
		map.put("advance", advancePayService.findAdvanceByOrderId(id));
		return "trade/advance_pay";
	}
	
	@ResponseBody
	@RequestMapping("/advance_buy")
	public String advance_buy(HttpServletRequest req) {
		try {
			advancePayService.saveOrderPay(super.getParaMap(req));
			return success("下单成功！！");
		} catch (Exception e) {
			e.printStackTrace();
			return fail("下单失败！！");
		}
	}
	
	@ResponseBody
	@RequestMapping("/advance_full")
	public String advance_full(String id) {
		try {
			advancePayService.fullAdvance(id);
			return success("确认成功！！");
		} catch (Exception e) {
			e.printStackTrace();
			return fail("确认失败！！");
		}
	}
	
	@RequestMapping("/advance_send_view")
	public String advance_send_view(String orderId, String customerId, Map map) {
		Map info = advancePayService.getAdvanceSendData(orderId, customerId);
		map.put("info", info);
		return "trade/advance_send";
	}
	
	@RequestMapping("/advance_scan")
	public String advance_scan(String send_code, String send_product_id, Map map) {
		System.err.println(send_code);
		System.err.println(send_product_id);
		Map code = advancePayService.findSendCode(Maps.newHashMap("code", send_code, "productIds", send_product_id));
		map.put("info", code);
		return "trade/advance/code";
	}
	
	@ResponseBody
	@RequestMapping("/advance_send")
	public String advance_send(HttpServletRequest req) {
		try {
			Map reqMap = super.getParaMap(req);
			reqMap.put("user", super.getUser(req));
			advancePayService.advanceSend(reqMap);
			return success("发货成功！！");
		} catch (Exception e) {
			e.printStackTrace();
			return fail("发货失败！！");
		}
	}
}
